<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Todo_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}

	public function view($userid,$month,$year){
		$this->db->distinct('date_id');
		$this->db->select('*');
		$this->db->group_by('date_id');		
		$this->db->from('to_do');
		$this->db->where('user_id',$userid);
		$this->db->where('month_id',$month);
		$this->db->where('year_id',$year);
		return $this->db->get()->result();		
	}
	
	public function todo($dateid,$month,$year,$user_id=null){		
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('date_id',$dateid);
		$this->db->where('month_id',$month);
		$this->db->where('year_id',$year);
		$this->db->where('is_fav',0);
		//$this->db->where('is_fav = 1');
		//return $this->db->last_query();
		return $this->db->get()->result();
	}
	
	public function get_today_fav($dateid,$month,$year,$user_id=null){
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		//$this->db->where('date_id',$dateid);
		$this->db->where('is_fav = 1 AND date_id ='.date('d'));
		$this->db->where('month_id',$month);
		$this->db->where('year_id',$year);
		return $this->db->get()->result();
	}
	
	
	public function fav_todo($dateid,$month,$year,$user_id=null){		
		//echo($month);
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('is_fav',1);
		$this->db->where('date_id <=',$dateid);
		$this->db->where('month_id =',$month);
		$this->db->where('year_id =',$year);
		$this->db->order_by('date_id ','ASC');
		//$this->db->join('users','to_do.user_id = users.id');
		//return $this->db->last_query();
		return $this->db->get()->result();
		}
		
	public function fav_todo_all($dateid,$month,$year,$user_id=null){		
		//echo($month);
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('is_fav',1);
		//$this->db->where('date_id <=',$dateid);
		//$this->db->where('month_id =',$month);
		$this->db->where('year_id =',$year);
		$this->db->order_by('date_id ','ASC');
		//$this->db->join('users','to_do.user_id = users.id');
		//return $this->db->last_query();
		return $this->db->get()->result();
		}
		
	public function todo_all($dateid,$month,$year,$user_id=null){		
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('date_id',$dateid);
		$this->db->where('month_id',$month);
		$this->db->where('year_id',$year);
		$this->db->where('is_fav',0);
		//$this->db->where('is_fav = 1');
		//return $this->db->last_query();
		return $this->db->get()->result();
	}
	
	
	public function delete($id){
		$this->db->delete('to_do',array('id'=>$id));
	}
	
	public function save($insert){
	$this->db->insert('to_do',$insert);
	}
	
	public function update($update,$id){
	$this->db->update('to_do',$update,array('id'=>$id));
	}
	
	public function complete($id,$dateid,$monthid,$yearid){
		$this->db->update('to_do',array('is_completed'=>'1','is_fav'=>0,'date_id'=>$dateid,'month_id'=>$monthid,'year_id'=>$yearid),array('id'=>$id));
	}
	
	public function incomplete($id){
		$this->db->update('to_do',array('is_completed'=>'0'),array('id'=>$id));
	}
	
	/// get the details of todo by id
	public function get_details($id){
		return $this->db->get_where('to_do',array('id'=>$id))->row_array();
	}
	
	public function add_worksheet($insert){
		$this->db->insert('worksheet',$insert);
	}
	
	/**
	* Founder part of todo
	*/
	public function view_all($get){
		$this->db->distinct('user_id');
		$this->db->select('to_do.user_id');
		$this->db->from('to_do');
		$this->db->where($get);		
		return $this->db->get()->result();	
	}
	
	public function add_fav($id){
		$this->db->update('to_do',array('is_fav' => 1),array('id'=>$id));
	}
	public function rem_fav($id,$date_id,$monthid,$yearid){
		$set = array(
		'is_fav' 	=> 0,
		'date_id' 	=> $date_id,
		'month_id'	=> $monthid,
		'year_id'	=> $yearid,
		);
		$this->db->update('to_do',$set,array('id'=>$id));
	}
	
	public function todo_fetch($dateid,$month,$year,$user_id=null){		
		//echo($month);
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		//$this->db->where('is_fav',1);
		$this->db->where('date_id <=',$dateid);
		$this->db->where('month_id =',$month);
		$this->db->where('year_id =',$year);
		//return $this->db->last_query();
		return $this->db->get()->result();
		}
		
	public function previous_todo($user_id=null){		
		$month=date('m');
		$user_id = isset($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('*');
		$this->db->from('to_do');
		$this->db->where('user_id',$user_id);
		$this->db->where('is_fav',1);
		$this->db->where('priority',NULL);
		$this->db->where('month_id !=',$month);
		$this->db->order_by('date_id ','ASC');
		return $this->db->get()->result();
		}


}
?>
